using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace Zuehlke.Blueprint.Quickstart.Architecture.Presentation.UserInterface.ValueConverters
{
    /// <summary>
    /// Converts an object to a value of type <see cref="Visibility"/>
    /// 
    /// object == null: Visibility.Collapsed
    /// object != null: Visibility.Visible
    /// 
    /// By providing a parameter with boolean value "true", the return values 
    /// are inverted.
    /// </summary>
    public class NullToVisibilityConverter : ValueConverterMarkupExtensionBase, IValueConverter
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets a value indicating whether the result gets inverted.
        /// </summary>
        public bool Invert { get; set; }

        #endregion

        #region IValueConverter Members

        /// <summary>
        /// See <see cref="IValueConverter.Convert"/>.
        /// </summary>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType != (typeof(Visibility)))
            {
                throw new NotSupportedException("Only target type Visibility supported!");
            }
            
            bool visible = (value != null);

            if (Invert)
            {
                visible = !visible;
            }

            return visible ? Visibility.Visible : Visibility.Collapsed;
        }

        /// <summary>
        /// See <see cref="IValueConverter.ConvertBack"/>.
        /// </summary>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}